home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / 1993-OCT.ZIP / ALLEY.ASC < prev    next >
Text File  |  1993-09-17  |  5KB  |  174 lines

  1. _ALGORITHM ALLEY_
  2. by Tom Swan
  3.  
  4. [LISTING ONE]
  5.  
  6. (* ----------------------------------------------------------- *(
  7. **  sample.pas -- Algorithm #12: Selection Sampling            **
  8. ** ------------------------------------------------------------**
  9. **  Creates a file SAMPLE.DAT with a specified number of names **
  10. **  extracted from Grady Ward's Moby Words. The first line of  **
  11. **  the output file indicates the number of selections.        **
  12. **  Assumes the number of names in the source is known.        **
  13. **  Reference: Knuth, Vol 2, p122                              **
  14. ** ------------------------------------------------------------**
  15. **     Copyright (c) 1993 by Tom Swan. All rights reserved.    **
  16. )* ----------------------------------------------------------- *)
  17.  
  18. program Sample;
  19. const
  20.   M = 21420;  { Number of records in source }
  21.   INFNAME = 'g:\moby\words\21400nam';  { Source file }
  22.   OUTFNAME = 'sample.dat'; { Destination file }
  23. var
  24.   infile, outfile: Text;  { File variables }
  25.   word: String;           { Holds each record from source }
  26.   requested,              { Requested number of samples }
  27.   examined,               { Total records examined }
  28.   selected: Integer;      { Total records selected }
  29.   r: Real;                { Random number 0 <= r < 1.0 }
  30. begin
  31.   Randomize;
  32.   Writeln('Write selected names to ', OUTFNAME);
  33.   Write('How many names? ');
  34.   Readln(requested);
  35.   if (requested <= 0) or (requested > M) then
  36.   begin
  37.     Writeln('Number must be >= 0 and <= ', M);
  38.     Exit
  39.   end;
  40.   examined := 0;
  41.   selected := 0;
  42.   Assign(infile, INFNAME);
  43.   Reset(infile);
  44.   Assign(outfile, OUTFNAME);
  45.   Rewrite(outfile);
  46.   Writeln(outfile, requested);  { Save 'requested' in file }
  47.   while (selected < requested) (* and (not Eof(infile)) *) do
  48.   begin
  49.     examined := examined + 1;
  50.     r := Random;
  51.     if (M - examined) * r >= requested - selected
  52.     then
  53.       Readln(infile)             { Skip next record }
  54.     else
  55.     begin                        { Select next record }
  56.       selected := selected + 1;  { Count selections so far }
  57.       Readln(infile, word);      { Read record from source }
  58.       Writeln(outfile, word);    { Write record to destination }
  59.       Writeln(word)              { Echo selection to display }
  60.     end
  61.   end;
  62.   Close(infile);
  63.   Close(outfile)
  64. end.
  65.  
  66.  
  67. [LISTING TWO]
  68.  
  69. (* ----------------------------------------------------------- *(
  70. **  pairings.pas -- Select sports-event team pairings          **
  71. ** ------------------------------------------------------------**
  72. **   This program generates team pairings for sports events.   **
  73. **   Each team is guaranteed to play each other team exactly   **
  74. **   once. No team will play more than one game per day.       **
  75. **   An asterisk ('*') means a day off for that team.          **
  76. **   For example, 5 teams produces this output:                **
  77. **     Day 1 - 12 34 5*                                        **
  78. **     Day 2 - 13 25 4*                                        **
  79. **     Day 3 - 14 2* 35                                        **
  80. **     Day 4 - 15 3* 24                                        **
  81. **     Day 5 - 1* 45 23                                        **
  82. ** ------------------------------------------------------------**
  83. **   Copyright (c) 1993 by Jim Mischel. All rights reserved.   **
  84. )* ----------------------------------------------------------- *)
  85.  
  86. program pairings;
  87. const
  88.   TEAMCOUNT = 5;
  89. var
  90.   TeamNames: Array [1 .. TEAMCOUNT + 1] of Char;
  91.   SwapArray: Array [1 .. TEAMCOUNT + 1] of Integer;
  92.   x, Temp, Day: Integer;
  93.   TempChar: Char;
  94. const
  95.   NTeams: Integer = TEAMCOUNT;
  96. begin
  97. { Set up team names. Normally read from a file. }
  98.   for x := 1 to NTeams do
  99.     TeamNames[x] := Chr(x + Ord('0'));
  100.   if Odd(NTeams) then
  101.   begin
  102.     NTeams := NTeams + 1;
  103.     TeamNames[NTeams] := '*'
  104.   end;
  105. { Set up the array that controls swapping. }
  106.   for x := 1 to NTeams do
  107.     SwapArray[x] := x;
  108.   for Day := 1 to NTeams - 1 do 
  109.   begin
  110.     Write('Day ', Day, ' -');
  111. { Write the team pairings for this day }
  112.     x := 1;
  113.     while x < NTeams do 
  114.     begin
  115.       Write(' ', TeamNames[x], TeamNames[x + 1]);
  116.       x := x + 2;
  117.     end;
  118.     WriteLn;
  119. { Perform swaps to prepare array for next day's pairings. }
  120.     if Odd(Day) 
  121.       then x := 2
  122.       else x := 3;
  123.     while x < NTeams do 
  124.     begin
  125.       TempChar := TeamNames[SwapArray[x]];
  126.       TeamNames[SwapArray[x]] := TeamNames[SwapArray[x + 1]];
  127.       TeamNames[SwapArray[x + 1]] := TempChar;
  128.       Temp := SwapArray[x];
  129.       SwapArray[x] := SwapArray[x + 1];
  130.       SwapArray[x + 1] := Temp;
  131.       x := x + 2
  132.     end
  133.   end
  134. end.
  135.  
  136.  
  137. Example 1: 
  138.  
  139. const
  140.   M = 1000;  { Input records }
  141.   N = 128;   { Subset (N <= M) }
  142. var
  143.   requested,
  144.   examined,
  145.   selected: Integer;
  146.   r: Real;
  147. begin
  148.   requested <- N;
  149.   examined <- 0;
  150.   selected <- 0;
  151.   while (selected < requested) do
  152.   begin
  153.     examined <- examined + 1;
  154.     r <- Random;
  155.     if (M - examined) * r 
  156.       >= (requested - selected)
  157.       then skip next input record
  158.     else begin
  159.       selected <- selected + 1;
  160.       use next input record
  161.     end
  162.   end
  163. end.
  164.  
  165.  
  166.  
  167. Example 2: 
  168.  
  169. for x <- 1 to NTeams - 1 do
  170.   for y <- x + 1 to NTeams do
  171.     write(x, '-', y, ',');
  172.  
  173.  
  174.